/*!
 * \file MediaConnectionsHandler.h
 * \brief Class managing established media connections
 */

#ifndef MGW_MEDIA_CONNECTIONS_HANDLER_H
#define MGW_MEDIA_CONNECTIONS_HANDLER_H

#include "MediaGenerator.h"
#include "common.pb.h"
#include <map>

namespace tsc
{
namespace mgw
{

/**
 * Class managing established media connections
 */
class MediaConnectionsHandler
{
public:

    static MediaConnectionsHandler& instance();

    static void init(common::GeneratorEnvironment& ge);

	virtual ~MediaConnectionsHandler();

	/**
	 * Finds MediaGenerator handling specified connection
	 * \param connectionId ID of connection to search
	 * \param gen pointer to generator to set if connection is found
	 * \return true if generator was found, false otherwise
	 */
	bool findConnection(uint32_t connectionId, MediaGenerator*& gen);

	/**
	 * Sets up new media connection. New MediaGenerator is created
	 * \param connectionId ID of new connection to establish
	 * \return true if connection was created, false otherwise
	 */
	bool setupConnection(
	        MediaGenerator*& mediaGen,
	        uint32_t connectionId,
	        ProtocolsCommon::IpAddress* clientIp,
	        uint32_t clientMediaPort,
	        uint32_t mediaItemId);

	/**
	 * Releases media connection. Transmission is stopped immediately
	 * socket is closed and MediaGenerator deleted
	 * \param connectionId ID of connection to release
	 * \return true if connection was successfuly removed, false otherwise
	 */
	bool releaseConnection(uint32_t connectionId);

private:
	/**
     * Creates new MediaConnectionsHandler
     */
    MediaConnectionsHandler(common::GeneratorEnvironment& ge);

    static MediaConnectionsHandler* _instance;

    std::map<uint32_t, MediaGenerator*> mConnMap;
    common::GeneratorEnvironment& _ge;
};

} // namespace mgw
} // namespace tsc

#endif // MGW_MEDIA_CONNECTIONS_HANDLER_H
